package me.jessyan.armscomponent.app.mvp.ui.activity.user;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import butterknife.BindView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;
import me.jessyan.armscomponent.commonres.widget.toast.ToastView;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import me.jessyan.armscomponent.commonsdk.utils.Utils;
import me.jessyan.armscomponent.app.R;
import me.jessyan.armscomponent.app.app.UserConstants;
import me.jessyan.armscomponent.app.di.component.DaggerUserSetUserinfoComponent;
import me.jessyan.armscomponent.app.mvp.contract.user.UserSetUserinfoContract;
import me.jessyan.armscomponent.commonsdk.core.User;
import me.jessyan.armscomponent.app.mvp.presenter.user.UserSetUserinfoPresenter;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * <p>
 * Author: Taozebi
 * Date: 08/19/2021 23:21
 * Description: describe of this class
 */

@Route(path = RouterHub.USER_SETUSERINFOACTIVITY)
public class UserSetUserinfoActivity extends BaseActivity<UserSetUserinfoPresenter> implements UserSetUserinfoContract.View {

    String username;
    String password;
    String smsCode;
    String inviteCode;
    String countryCode;
    String paypassword;

    @BindView(R.id.user_et_realname)
    EditText user_et_realname;

    @BindView(R.id.user_rg_register_sex)
    RadioGroup user_rg_register_sex;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserSetUserinfoComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.user_activity_set_userinfo; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        loadParams();
        loadTitle();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ToastView.getInstance().showToast(this,message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick({R.id.user_btn_next_step})
    public void onClicked(View v) {
        int id = v.getId();
        if(id == R.id.user_btn_next_step){
            if (Utils.isFastClick()) {
//                Utils.navigation(this, RouterHub.USER_TESTKNOWLEDGELEVELACTIVITY);
//                finish();
//                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                mPresenter.register();
            }
        }
    }

    @OnCheckedChanged({R.id.user_rg_register_man, R.id.user_rg_register_woman})
    public void onCheckedChange(CompoundButton buttonView, boolean isChecked){
        if(isChecked){
            int id = buttonView.getId();
            if(id == R.id.user_rg_register_man){
            }else if(id == R.id.user_rg_register_woman){

            }
        }
    }



    private void loadParams(){
        username = getIntent().getStringExtra(UserConstants.USER_NAME);
        password = getIntent().getStringExtra(UserConstants.PASSWORD);
        smsCode = getIntent().getStringExtra(UserConstants.SMS_CODE);
        inviteCode = getIntent().getStringExtra(UserConstants.INVITE_CODE);
        countryCode = getIntent().getStringExtra(UserConstants.COUNTRY_CODE);
        paypassword = getIntent().getStringExtra(UserConstants.PAY_PASSWORD);
    }


    private void loadTitle() {
        String title = "设置相关信息";
        setTitle(title);
    }

    @Override
    public String getUserName() {
        return username;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getPayPassword() {
        return paypassword;
    }

    @Override
    public String getSmsCaptcha() {
        return smsCode;
    }

    @Override
    public String getInviteCode() {
        return inviteCode;
    }

    @Override
    public String getRealName() {
        return user_et_realname.getText().toString().trim();
    }

    @Override
    public int getGender() {
        return user_rg_register_sex.getCheckedRadioButtonId() == R.id.user_rg_register_man ? 1:0;
    }

    @Override
    public String getCountryCode() {
        return countryCode;
    }

    @Override
    public void registerSuccess(User user) {
        ToastView.getInstance().showToast(this,R.drawable.public_ic_prompt_complete,"欢迎注册启明星，请重新登录~");
        toLogin(true);
    }

    @Override
    public void registerFailure() {
        ToastView.getInstance().showToastError(this,"用户注册失败~");
    }

    private void toLogin(boolean isPasswordLogin) {
        ARouter.getInstance().build(RouterHub.USER_LOGINACTIVITY)
                .withBoolean("isPasswordLogin",isPasswordLogin)
                .withString("username",username)
                .withString("password",password)
                .navigation();
        finish();
        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }
}
